import { _decorator, Component, errorID, Label, Node, Sprite, SpriteFrame } from 'cc';
import { GameManager } from './GameManager';
import { BundleManager } from '../AshFramework/Managers/BundleManager';
import { WeaponBattleValue, weaponBuyPrice } from '../AshFramework/Datas/GameConstant';
import { BattleManager } from './BattleManager';
import { Chariot } from './Chariot';
const { ccclass, property } = _decorator;

@ccclass('WeaponBuyer')
export class WeaponBuyer extends Component {

    private _weaponSprite: Sprite = null;
    private _priceLabel: Label = null;

    type: number;//武器种类
    layer: number;//第几层
    price: number;//价格

    protected onLoad(): void {
        this._weaponSprite = this.node.getChildByName("武器图片").getComponent(Sprite);
        this._priceLabel = this.node.getChildByName("Price").getComponent(Label);
    }

    init(type: number, layer: number) {
        let name = `${GameManager.Instance.userData.epoch}武器${type}`;
        this._weaponSprite.spriteFrame = BundleManager.AssetMap.get(name) as SpriteFrame;
        this.price = weaponBuyPrice[GameManager.Instance.userData.epoch][type - 1];//武器价格
        this._priceLabel.string = String(this.price);
        this.type = type;
        this.layer = layer
    }

    click() {
        BattleManager.Instance.curEnergy -= this.price;
        Chariot.Instance.createWeapon(this.type, this.layer);
        BattleManager.Instance.updateEnergy();
    }

    protected onEnable(): void {
        this.node.on("click", this.click, this);
    }

}


